<template>
  <div class="setPayPassword">
    <u-nav-bar :title="'设置支付密码'"></u-nav-bar>
    <div class="setPayPassword__ct">
      <div class="title">请设置您的支付密码</div>
      <div class="memo">支付密码用于钱包支付。为了资金安全，请牢记支付密码切不要透露给他人</div>

      <van-form v-model="currentModel" @submit="commit">
        <van-field
          v-model="currentModel.newPayPassword"
          :maxlength="6"
          type="number"
          placeholder="请设置6位数字密码"
          :rules="[
            {
              required: true,
              message: '支付密码不能为空'
            }
          ]"
        ></van-field>
        <van-field
          v-model="currentModel.oldPayPassword"
          :maxlength="6"
          placeholder="请再次输入密码"
          :rules="[
            {
              required: true,
              message: '二次支付密码不能为空'
            },
            {
              validator: passwordValidator,
              message: '两次密码不一致'
            }
          ]"
        ></van-field>
        <van-button :class="canCommit ? 'commit__acitve' : ''" native-type="submit">完成</van-button>
      </van-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, watchEffect } from 'vue'
import { useRouter } from 'vue-router'
import store from '@/store/index'
import { Toast } from 'vant'
import uNavBar from '@/components/NavBar.vue'
import { PayPasswordModel } from '@/store/user/types'

type DataModel = {
  currentModel: PayPasswordModel
  canCommit: boolean
}

export default defineComponent({
  name: 'SetPayPassword',

  components: {
    'u-nav-bar': uNavBar
  },
  setup() {
    let data = reactive<DataModel>({
      currentModel: {
        newPayPassword: '',
        oldPayPassword: ''
      } as PayPasswordModel,
      canCommit: false
    })

    const router = useRouter()

    // watchEffect

    watchEffect(() => {
      if (
        data.currentModel.newPayPassword &&
        data.currentModel.newPayPassword === data.currentModel.oldPayPassword &&
        data.currentModel.newPayPassword.length === 6
      ) {
        data.canCommit = true
      } else {
        data.canCommit = false
      }
    })

    /** 事件区域 */

    const commit = async (): Promise<void> => {
      if (!data.canCommit) {
        if (data.currentModel.newPayPassword.length !== 6) {
          Toast.fail('密码必须是6位数字')
        }
        return
      }
      let params = JSON.parse(JSON.stringify(data.currentModel))
      params.oldPayPassword = undefined
      const result = await store.dispatch('user/handleSetFinanceWalletDriverPayPassword', params)
      if (result) {
        Toast.success('设置成功')
        router.go(-1)
      }
    }

    const passwordValidator = (value: any): any => {
      if (data.currentModel.newPayPassword !== value) {
        return false
      } else {
        return true
      }
    }

    /** 操作区域 */

    return {
      ...toRefs(data),
      commit,
      passwordValidator
    }
  }
})
</script>
<style lang="scss" scoped>
@import '@/style/variables.scss';
@import '@/style/mixins.scss';
.setPayPassword {
  @include flex-cloumn-width-height-100;
  background-color: white;

  &__ct {
    @include flex-scroll-content-pd(30px 25px 12px 25px);
    position: relative;
    background-color: #fcfcfc;
    border-radius: 20px 20px 0 0;
    .van-cell {
      margin-bottom: 15px;
      background: #ffffff;
      border-radius: 6px;
      line-height: 30px;
      box-sizing: content-box;
      padding: 12px;
      width: calc(100% - 24px);
    }
    .van-cell::after {
      border: unset;
    }
    button {
      height: 48px;
      width: 100%;
      background: linear-gradient(125deg, #74cdff 0%, #409eff 100%);
      box-shadow: 0px 3px 12px 0px rgba(64, 158, 255, 0.4);
      border-radius: 24px;
      color: white;
      border: none;
      margin-top: 20px;
      opacity: 0.5;
    }
    .commit__acitve {
      opacity: 1;
    }
    .title {
      font-size: 20px;
      font-weight: 500;
      color: #101736;
      line-height: 28px;
    }
    .memo {
      font-size: 12px;
      font-weight: 400;
      color: #9fa2af;
      line-height: 17px;
      margin-bottom: 70px;
    }
  }
}
</style>
